const app = getApp()
const db = wx.cloud.database();
let name = null; //变量，用于存放用户输入的账号
let student_id = null; //变量，用于存放用户输入的密码
let openid = null; //变量，用于存放用户openid
Page({
  data: {
    userInfo: {}, //变量，用于存放用户微信姓名和头像
    hasUserInfo: false,
    canIUseGetUserProfile: false,
  },
  //输入用户名
  inputName: function (event) {
    name = event.detail.value //将用户输入的账号放到变量里面
  },
  //输入密码
  inputPassword(event) {
    student_id = event.detail.value //将用户输入的密码放到变量里面
  },
  onLoad() {
    // this.onGetOpenid();
    // this.addData()
    console.log(app.globalData)

  },
  addData() {
    db.collection("teacher_users").add({
      data: {
        class_list: [
          //上课长度全部默认为两节课
          {
            "weak": 1,
            "class_order": 1,
            "class_length": 2,
            "subject": "单片机",
            "address": "五教208",
            "weak_work": "1~16周"
          },

          {
            "weak": 2,
            "class_order": 8,
            "class_length": 2,
            "subject": "汇编语音",
            "address": "五教208",
            "weak_work": "1~16周"
          },

          {
            "weak": 3,
            "class_order": 3,
            "class_length": 2,
            "subject": "单片机",
            "address": "五教208",
            "weak_work": "1~16周"
          },
          {
            "weak": 3,
            "class_order": 5,
            "class_length": 4,
            "subject": "C语言",
            "address": "五教208",
            "weak_work": "1~16周"
          },

          {
            "weak": 6,
            "class_order": 9,
            "class_length": 2,
            "subject": "C语言实验课",
            "address": "五教208",
            "weak_work": "1~16周"
          },

        ],
        name: "张永辉",
        phtot: "17325647895",
        teacher: "123456",
        institute: "信息与通信工程学院",
      }
    }).then(res => {
      console.log(res)
      console.log('更新数据库完成')
    })

  },
  //  查询数据库中是否有学号和姓名，如果有，检查是否有openid,如果没有，则写入openid,跳转页面
  login() {
    // console.log(app.globalData)

    wx.cloud.callFunction({
        name: "bindTeacher",
        data: {
          name: name,
          teacher: student_id,
        }
      })
      .then(res => {
        console.log(res)
        console.log(res.result.data[0])
        if (res.result.data.length) {

          if (res.result.data[0].hasOwnProperty("openid")) {
            wx.showModal({
              title: '提示',
              content: '该账户已绑定',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            console.log('绑定成功')
            console.log(res.result.data[0]._id)
            db.collection('teacher_users').doc(res.result.data[0]._id).update({
              // data 传入需要局部更新的数据
              data: {
                openid: app.globalData.openid,
                userInfo: app.globalData.userInfo,
              },
              success: function (res) {
                console.log(res)
                wx.showModal({
                  title: '提示',
                  content: '绑定成功',
                  showCancel: false,
                  success(res) {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '../tabs/tabs', //登录成功后要跳转的页面
                      })
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              }
            })

            // db.collection("teacher_users").doc(res.result.data[0]._id).update({
            //   data: {
            //     openid: app.globalData.openid,

            //   }
            // }).then(res => {
            //   console.log(res)
            //   wx.showModal({
            //     title: '提示',
            //     content: '绑定成功',
            //     showCancel: false,
            //     success(res) {
            //       if (res.confirm) {
            //         wx.navigateTo({
            //           url: '../tabs/tabs',//登录成功后要跳转的页面
            //         })
            //       } else if (res.cancel) {
            //         console.log('用户点击取消')
            //       }
            //     }
            //   })

            // })
          }


        } else {




          wx.showModal({
            title: '提示',
            content: '姓名或学号错误',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })



        }


      })


  }

})